/*
 * Copyright 2008 Federal Emergency Management Agency
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 *    http://www.apache.org/licenses/LICENSE-2.0 
 *    
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 *  
 */
package gov.fema.hazus.bdc.client.hazards.flood.basement;

import gov.fema.hazus.bdc.client.common.ui.AbstractForm;
import gov.fema.hazus.bdc.client.common.ui.BuildingUpdate;
import gov.fema.hazus.bdc.client.common.ui.GenericAnswerListBox;
import gov.fema.hazus.bdc.client.common.ui.TextBoxDoubleValueBlurHandler;
import gov.fema.hazus.bdc.client.common.ui.TextBoxIntegerValueBlurHandler;
import gov.fema.hazus.bdc.client.hazards.flood.FloodConstants;
import gov.fema.hazus.bdc.shared.entity.Building;
import gov.fema.hazus.bdc.shared.entity.FloodBasement;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;

public class FloodBasementForm extends AbstractForm implements BuildingUpdate {
	
	private Label levels;
	private Label percentageContentsValue;
	private Label percentageFinished;
	private Label dryFloodProofed;
	private Label floodProofingElevation;
	private Label enclosureType;
	
	// form fields
	private TextBox formLevels = new TextBox();
	private TextBox formPercentageContentsValue = new TextBox();
	private TextBox formPercentageFinished = new TextBox();
	private GenericAnswerListBox formDryFloodProofed = new GenericAnswerListBox();
	private TextBox formFloodProofingElevation = new TextBox();
	private EnclosureTypeListBox formEnclosureType = new EnclosureTypeListBox();
	
	// ui components
	private DockPanel docker;
	private Grid table = new Grid(6,2);
	
	// entities
	private FloodBasement floodBasement;
	
	// listeners
	private FloodBasementDesignTextBoxKeyListener floodBasementDesignTextBoxKeyListener;
	private FloodBasementListBoxChangeListener floodBasementListBoxChangeListener;
	private TextBoxDoubleValueBlurHandler textBoxDoubleValueBlurHandler;
	private TextBoxIntegerValueBlurHandler textBoxIntegerValueBlurHandler;
	
	private FloodConstants constants;
	
	public FloodBasementForm(Building building)
	{
		// do parent
		super(building);
		
		// set up widgets
		docker = new DockPanel();
		
		// set up tab title
		constants = (FloodConstants)GWT.create(FloodConstants.class);
		tabTitle = constants.floodBasementTabTitle();
		
		levels = new Label(constants.floodBasementLevels());
		percentageContentsValue = new Label(constants.floodBasementPercentageContentsValue());
		percentageFinished = new Label(constants.floodBasementPercentageFinished());
		dryFloodProofed = new Label(constants.floodBasementDryFloodProofed());
		floodProofingElevation = new Label(constants.floodBasementFloodProofingElevation());
		enclosureType = new Label(constants.floodBasementEnclosureType());
		
		// add labels to form
		table.setWidget(0, 0, levels);
		table.setWidget(1, 0, percentageContentsValue);
		table.setWidget(2, 0, percentageFinished);
		table.setWidget(3, 0, dryFloodProofed);
		table.setWidget(4, 0, floodProofingElevation);
		table.setWidget(5, 0, enclosureType);
		
		// set up form fields
		floodBasement = building.getFloodBasement();
		floodBasementDesignTextBoxKeyListener = new FloodBasementDesignTextBoxKeyListener(floodBasement);
		floodBasementListBoxChangeListener = new FloodBasementListBoxChangeListener(floodBasement);
		textBoxDoubleValueBlurHandler = new TextBoxDoubleValueBlurHandler();
		textBoxIntegerValueBlurHandler = new TextBoxIntegerValueBlurHandler();
		
		// listeners
		loadListeners();
		
		// forms
		loadForms();
		
		// add fields to table
		table.setWidget(0, 1, formLevels);
		table.setWidget(1, 1, formPercentageContentsValue);
		table.setWidget(2, 1, formPercentageFinished);
		table.setWidget(3, 1, formDryFloodProofed);
		table.setWidget(4, 1, formFloodProofingElevation);
		table.setWidget(5, 1, formEnclosureType);
		
		// add table to docker
		docker.add(table, DockPanel.CENTER);
		
		initWidget(docker);	
		
	}

	public void updateBuilding(Building building) {

		// entities
		setBuilding(building);
		this.floodBasement = building.getFloodBasement();
		
		// listeners
		floodBasementDesignTextBoxKeyListener.updateFloodBasement(floodBasement);
		floodBasementListBoxChangeListener.updateFloodBasement(floodBasement);
		
		// reload forms
		loadForms();
		
	}
	
	private void loadForms()
	{

		if( floodBasement != null)
		{
			
			String levels = null;
			if (floodBasement.getLevels() != null) {
				levels = floodBasement.getLevels().toString();
			}
			formLevels.setText( levels );
			formLevels.setName(FloodBasement.LEVELS);
			
			String percentageContentsValue = null;
			if (floodBasement.getPercentageContentsValue() != null) {
				percentageContentsValue = floodBasement.getPercentageContentsValue().toString();
			}
			formPercentageContentsValue.setText( percentageContentsValue);
			formPercentageContentsValue.setName(FloodBasement.PERCENTCONTENTS);
			
			String percentageFinished = null;
			if (floodBasement.getPercentageFinished() != null) {
				percentageFinished = floodBasement.getPercentageFinished().toString();
			}
			formPercentageFinished.setText( percentageFinished );
			formPercentageFinished.setName(FloodBasement.PERCENTFINISHED);
			
			formDryFloodProofed.setListItem(floodBasement.getDryFloodProofed());
			formDryFloodProofed.setName(FloodBasement.DRYFLOOD);
			
			String floodProofingElevation = null;
			if (floodBasement.getFloodProofingElevation() != null) {
				floodProofingElevation = floodBasement.getFloodProofingElevation().toString();
			}
			formFloodProofingElevation.setText( floodProofingElevation );
			formFloodProofingElevation.setName(FloodBasement.FLOODPROOFINGELEVATION);
			
			formEnclosureType.setListItem(floodBasement.getEnclosureType());
			formEnclosureType.setName(FloodBasement.ENCLOSURETYPE);
		
		}
		
		if( building != null)
		{
	        if( building.getMode().equalsIgnoreCase(Building.READ_ONLY))
	        {
	            formLevels.setEnabled(false);
	            formPercentageContentsValue.setEnabled(false);
	            formPercentageFinished.setEnabled(false);
	            formDryFloodProofed.setEnabled(false);
	            formFloodProofingElevation.setEnabled(false);
	            formEnclosureType.setEnabled(false);            
	        }
	        else
	        {
	            formLevels.setEnabled(true);
	            formPercentageContentsValue.setEnabled(true);
	            formPercentageFinished.setEnabled(true);
	            formDryFloodProofed.setEnabled(true);
	            formFloodProofingElevation.setEnabled(true);
	            formEnclosureType.setEnabled(true);               
	        }
		}
				
	}
	
	private void loadListeners()
	{
		
		formLevels.addFocusListener(floodBasementDesignTextBoxKeyListener);
		formLevels.addBlurHandler(textBoxDoubleValueBlurHandler);
		
		formPercentageContentsValue.addFocusListener(floodBasementDesignTextBoxKeyListener);
		formPercentageContentsValue.addBlurHandler(textBoxDoubleValueBlurHandler);
		
		formPercentageFinished.addFocusListener(floodBasementDesignTextBoxKeyListener);
		formPercentageFinished.addBlurHandler(textBoxDoubleValueBlurHandler);
		
		formDryFloodProofed.addChangeListener(floodBasementListBoxChangeListener);
		
		formFloodProofingElevation.addFocusListener(floodBasementDesignTextBoxKeyListener);
		formFloodProofingElevation.addBlurHandler(textBoxIntegerValueBlurHandler);
		
		formEnclosureType.addChangeListener(floodBasementListBoxChangeListener);
	}

}
